/*
 * ClipRouteEndAction.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.admin.actions;

import org.fpdev.core.transit.SubRoute;
import org.fpdev.core.transit.TransitPath;
import org.fpdev.apps.admin.AdminClient;

/**
 *
 * @author demory
 */
public class ClipRouteEndAction extends TransitPathModificationAction {

  private int endPLI_; // pli of the path's new last link

  public ClipRouteEndAction(SubRoute sub,  int endPLI) {
    super(sub);
    endPLI_ = endPLI;
  }

  protected boolean modifyTransitPath(AdminClient av) {
    TransitPath path = sub_.getPath();
  
     if(endPLI_ == 0) { // special case: path being shortened to single link
      TransitPath newPath = new TransitPath(av.getEngine(), sub_, path.getType());
      newPath.addDirectedFirstLink(path.startLink(), path.startNode());
      sub_.setPath(newPath);
      return true;
    }
    
    int curPLI = path.linkCount()-1;
    while ( curPLI > endPLI_) {
      if (path.stopPrecedes(curPLI))
        path.deleteStop(path.getStopIndexFromInPLI(curPLI - 1)); 
      path.removeLastLink();
      curPLI--;
    }
    return true;
  }

  public String getName() {
    return "Clip Path from End";
  }
}
